package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.retain.RetainConfigVO;
import com.qumitech.visualnovel.pubdef.dto.retain.RetainConfigDTO;
import com.qumitech.visualnovel.service.retain.RetainConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/retain")
public class RetainController {

    @Autowired
    RetainConfigService retainConfigService;


    @GetMapping(value = "")
    public BaseRsp listRetainConfig(@RequestParam(value = "name", required = false) String name,
                                    @RequestParam(value = "grouping", required = false) String grouping,
                                    @RequestParam(value = "status", required = false) Integer status,
                                    @RequestParam(value = "createTime", required = false) String createTime,
                                    @RequestParam(value = "orderBy", defaultValue = "create_time") String orderBy,
                                    @RequestParam(value = "orderType", defaultValue = "desc") String orderType,
                                    @RequestParam(value = "offset", defaultValue = "0") int offset,
                                    @RequestParam(value = "limit", defaultValue = "20") int limit) {
        List<RetainConfigVO> data = retainConfigService.listRetainConfig(name, grouping, status, createTime, orderBy, orderType, offset, limit);
        int count = retainConfigService.countRetainConfig(name, grouping, status, createTime);
        Map<String, Object> rep = new HashMap<>();
        rep.put("count", count);
        rep.put("items", data);
        return new BaseRsp(ResultEnum.SUCCESS, rep);
    }

    @PostMapping("")
    public BaseRsp updateRetainConfig(@RequestBody RetainConfigDTO retainConfigDTO) {
        return retainConfigService.updateRetainConfig(retainConfigDTO);
    }

    @DeleteMapping("")
    public BaseRsp deleteRetainConfig(@RequestParam(value = "id") Integer id) {
        retainConfigService.deleteRetainConfig(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PostMapping("/status")
    public BaseRsp ModifyRetainConfigStatus(@RequestBody RetainConfigDTO retainConfigDTO) {
        Integer id = retainConfigDTO.getId();
        Integer status = retainConfigDTO.getStatus();
        if (id == null || status == null) {
            return new BaseRsp(401, "参数不全");
        }
        retainConfigService.ModifyRetainConfigStatus(retainConfigDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/groups")
    public BaseRsp ListRetainGroupings() {
        List<HashMap<String, Object>> data = retainConfigService.listRetainGroupings();
        return new BaseRsp(ResultEnum.SUCCESS, data);
    }

}

